package com.zzy.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzy.product.entity.AttrEntity;
import com.zzy.product.entity.AttrGroupEntity;
import com.zzy.product.service.CategoryBrandRelationService;
import com.zzy.utils.utils.PageUtils;
import com.zzy.utils.utils.Query;
import com.zzy.product.mapper.CategoryMapper;
import com.zzy.product.entity.CategoryEntity;
import com.zzy.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author KAZU
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements CategoryService {
	
	@Autowired
	private CategoryMapper categoryMapper;
	
	@Autowired
	private CategoryBrandRelationService categoryBrandRelationService;
	
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }
	
	/**
	 * 查出所有分类以及子分类，以树形结构组装起来
	 */
	@Override
	public List<CategoryEntity> listWithTree() {
		// 查出所有分类
		List<CategoryEntity> list = categoryMapper.selectList(null);
		// 组装成父子的树形结构
		// 1 找到一级分类
		List<CategoryEntity> level1Menus = list.stream().filter((categoryEntity) -> {
			return categoryEntity.getParentCid() == 0;
		}).map((menu) -> {
			/*找到子菜单，递归*/
			menu.setChildren(getChild(menu, list));
			return menu;
		}).sorted((menu1, menu2) -> {
			/*排序*/
			return  (menu1.getSort() == null ? 0 : menu1.getSort()) -
					(menu2.getSort() == null ? 0 : menu2.getSort());
		}).collect(Collectors.toList());
		
		
		return level1Menus;
	}
	
	/**
	 * 删除
	 * @param asList 要删除的id
	 */
	@Override
	public void removeMenuByIds(List<Long> asList) {
		// TODO: 检查当前删除的菜单、是否被其他地方引用
		categoryMapper.deleteBatchIds(asList);
		
	}
	
	/**
	 * 返回完整路徑
	 * @param catelogId
	 * @return
	 */
	@Override
	public Long[] findCatelogPath(Long catelogId) {
		List<Long> paths = new ArrayList<>();
		List<Long> path = findParentPath(catelogId, paths);
		Collections.reverse(path);
		return (Long[]) path.toArray(new Long[path.size()]);
	}
	
	/**
	 * 级联更新所有关联数据
	 * @param category
	 */
	@Transactional
	@Override
	public void updateCascade(CategoryEntity category) {
		this.updateById(category);
		categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
	}
	
	private List<Long> findParentPath(Long catelogId, List<Long> paths) {
		/*收集当前节点id*/
		paths.add(catelogId);
		CategoryEntity byId = this.getById(catelogId);
		if (byId.getParentCid() != 0) {
			findParentPath(byId.getParentCid(), paths);
		}
		return paths;
	}
	
	/**
	 * 递归查找所有菜单的子菜单
	 * */
	private List<CategoryEntity> getChild(CategoryEntity root, List<CategoryEntity> all) {
		
		return all.stream().filter((categoryEntity) -> {
			return categoryEntity.getParentCid().equals(root.getCatId());
		}).map((categoryEntity) -> {
			categoryEntity.setChildren(getChild(categoryEntity, all));
			return categoryEntity;
		}).sorted((menu1, menu2) -> {
			return  (menu1.getSort() == null ? 0 : menu1.getSort()) -
					(menu2.getSort() == null ? 0 : menu2.getSort());
		}).collect(Collectors.toList());
	}
	
}